<?php

namespace Form\System;

use App\Dao\Exam\ExamDao;
use App\Dao\System\SystemChangelogDao;
use App\Dao\Task\TaskHelpDao;
use App\Dao\Task\TaskTypesDao;
use Form\BaseForm;
use App\Model\Task\TaskHelpCategory;
use Yj\Apidog\Annotation\ApiController;
use Yj\Apidog\Annotation\FormData;
use Yj\Apidog\Annotation\PostApi;
use Yj\ColumnBuilder\Column;
use Yj\ColumnBuilder\Table;
use Yj\ColumnBuilder\Templates\Field;
use Yj\ColumnBuilder\Templates\FieldTemplate;
use Yj\ColumnBuilder\Templates\InputTemplate;
use Yj\Form\Elm;

/**
 * @ApiController(prefix="form/system/changelog")
 */
class ChangeLog extends \Yj\Form\BaseForm
{
    protected $yjCode = 'system/changelog';

    public function getColumn()
    {
        $colum = Column::create();

        $colum->setRules([

        ]);

        return $colum->toArray();
    }

    public function getTabs(){
        return Table::header()->setProp('status','all')->setOptions([
            ['label'=>'全部','value'=>'all'],
            ['label'=>'代付款','value'=>'0'],
            ['label'=>'待审核','value'=>'1'],
            ['label'=>'进行中','value'=>'2'],
            ['label'=>'已驳回','value'=>'-1'],
        ])->toArray();
    }

    public function getSearch(){
        return Table::search()->setFields([
            Field::input('名称','keyword')->setLabel('名称')->setVModel('keyword'),
            Field::select()->setLabel('支付状态')->setVModel('paystatus')->setOptions([
                ['label'=>'已付款','value'=>'1'],
                ['label'=>'未付款','value'=>'0'],
                ['label'=>'全部','value'=>null],
            ])
        ])->toArray();
    }

    /**
     * @PostApi(path="config",description="配置")
     */
    public function config()
    {
        $routes = $this->getRoute();
        $column = $this->getColumn();
        $search = $this->getSearch();
        $tabs = $this->getTabs();
        _SUCCESS(compact('routes', 'column','tabs','search'));
    }

    /**
     * @PostApi(path="update",description="添加")
     * @FormData(key="id",rule="nullable")
     */
    public function update()
    {
        $id = $this->getParams()->get('id');
        $form = Elm::createForm($this->getRoute($id ? 'edit' : 'add'));
        $form->setRule([
            Elm::input('version','版本号')->required(),
            Elm::scEditor()->title('更新内容')->field('content'),
        ]);
        $id && $form->append(
            Elm::hidden('id', $id)
        );
        $lists = $form->setTitle(($id ? '编辑' : '添加') . '更新日志')->formData(function () use ($id) {
            $formData = [];
            if ($id) {
                $formData = App(SystemChangelogDao::class)->read($id);
            }
            return $formData;
        });
        _SUCCESS(formToData($lists));
    }
}